import { GlobalData } from "../Common/GlobalData";
import { SceneGame } from "../SceneGame";
import BaseView from "./BaseView";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BaseDrag extends BaseView {

    @property({ type: cc.Node, tooltip: "触摸层" })
    protected touchLayer: cc.Node = null;
    @property({ type: cc.Node, tooltip: "点击的小方块" })
    protected clickRect: cc.Node = null;

    protected rootScale: number = 1;

    protected onLoad() {
        super.onLoad();

        this.rootScale = SceneGame.instance.getRootScale();
    }

    protected onEnable() {
        this.touchLayer.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.touchLayer.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.touchLayer.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        this.touchLayer.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
    }
    protected onDisable() {
        this.touchLayer.off(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.touchLayer.off(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.touchLayer.off(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        this.touchLayer.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
    }

    public getClickPos(event: cc.Event.EventTouch) {
        const pos = event.getLocation();
        const x = (pos.x - GlobalData.stage_width / 2) / this.rootScale;
        const y = (pos.y - GlobalData.stage_height / 2) / this.rootScale;
        this.clickRect.setPosition(cc.v2(x, y));
        return { x, y };
    }

    public touchStart(event: cc.Event.EventTouch) { }
    public touchMove(event: cc.Event.EventTouch) { }
    public touchEnd(event: cc.Event.EventTouch) { }
}